package com.java.struts.order.dao;

import com.java.struts.base.util.HibernateUtil;
import com.java.struts.order.entity.OrderinfoEntity;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import java.util.List;

public class OrderDao {
    /**
     * 查询所有的订单信息
     * @return
     */
    public List<OrderinfoEntity> listType(){
        //  获取query对象
        Query query = HibernateUtil.getQuery("from OrderinfoEntity");
        // 查询
        List<OrderinfoEntity> list = (List<OrderinfoEntity>)query.list();
        return list;
    }

    /**
     * 添加订单
     * @param orderInfo
     */
    public void addOrder(OrderinfoEntity orderInfo){
        //  获取session对象
        Session session  = HibernateUtil.getSession();
        //　开启事务
        Transaction transaction = session.beginTransaction();
        // 存储对象
        session.save(orderInfo);
        transaction.commit();
        session.close();
    }

    /**
     * 删除订单信息
     * @param orderId
     */
    public void deleteOrder(Integer orderId){

        Session session = HibernateUtil.getSession();

        Transaction transaction = session.beginTransaction();
        //　获取query对象
        Query query = HibernateUtil.getQuery("delete from OrderinfoEntity where oid=?1");

        //　给？号赋值
        query.setParameter(1,orderId);
        // 执行
        query.executeUpdate();
        // 执行事务
        transaction.commit();
        // 关闭session
        session.close();


    }

    /**
     *  修改订单信息
     * @param orderInfo
     */
    public void updateType(OrderinfoEntity orderInfo){
        //  获取session对象
        Session session  = HibernateUtil.getSession();

        session.update(orderInfo);

        session.close();
    }
}
